package com.mojo.io.net;

import java.io.*;
import java.net.*;
import java.util.HashMap;

/**
 * java网络支持
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/29 15:42
 */
public class InternetDemo {
    public static void main(String[] args) throws IOException {
        iPtest();
        urltest();
        socketsTest();
    }

    private static void socketsTest() throws IOException {

        Socket socket = new Socket(InetAddress.getByName("10.0.12.245"), 8080);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        out.write("hello,world".getBytes());
        ServerSocket serverSocket = new ServerSocket(8080, 200);
        Socket accept = serverSocket.accept();
        InputStream acceptIn = accept.getInputStream();
        byte[] b = new byte[1024];
        acceptIn.read(b);

    }

    private static void iPtest() throws UnknownHostException {
        InetAddress byName = InetAddress.getByName("http://www.baidu.com");
        System.out.println(byName);
    }

    private static void urltest() throws IOException {
        URL url = new URL("http://www.baidu.com");
        //字节流
        BufferedInputStream buffer = new BufferedInputStream(url.openStream());
        //字符流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
        String s;
        while ((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
        }
    }
}
